package sub

import (
	// "crypto/internal/edwards25519/field"
	"fmt"
	"strings"
	// import ./util as u
	// 🦜 : for DiscoverGateway()
)

var (
	S_BLUE  = "\033[94m"
	S_CYAN  = "\033[96m"
	S_GREEN = "\033[92m"
	S_NOR   = "\033[0m"
)

func U_debugF(format string, args ...interface{}) {
	fmt.Printf(format, args...)
}

func U_infoF(format string, args ...interface{}) {
	fmt.Printf(format, args...)
}

// HeaderContentResult represents the result of get_next_header_content
// U_u01_ProcessMD processes markdown content and filters headers based on allowlist
func U_u01_ProcessMD(s string, L0 []string) []string {
	/*
	   s: the markdown content as a string
	   L0: the allowlist of header keywords
	   return the filtered markdown lines
	*/
	// 1. split the md
	ln := strings.Split(s, "\n")
	U_infoF("Step 1: Split into %s%d%s lines\n", S_BLUE, len(ln), S_NOR)

	// 2. remove empty lines
	var ln1 []string // tmp
	for _, l := range ln {
		t := strings.TrimSpace(l)
		if t != "" {
			ln1 = append(ln1, t)
		}
	}
	ln = ln1
	U_infoF("Step 2: After removing empty lines: %s%d%s lines\n", S_GREEN, len(ln), S_NOR)

	// 3. get the filter lines
	var o []string
	var h string
	var c []string

	// getNextHeaderContent extracts the next header and its content from lines
	getNextHeaderContent := func(ln []string) (string, []string, []string) { // header, content, remains
		U_debugF("=== Starting getNextHeaderContent ===\n")
		U_debugF("Input lines length: %s%d%s\n", S_BLUE, len(ln), S_NOR)

		// eat_till_header function
		eatTillHeader := func(s []string) ([]string, []string) {
			var c []string
			U_debugF("  eatTillHeader: processing %s%d%s lines\n", S_CYAN, len(s), S_NOR)

			for len(s) > 0 && !strings.HasPrefix(s[0], "# ") {
				c = append(c, s[0])
				U_debugF("  appended %s%s%s in eatTillHeader\n", S_GREEN, s[0], S_NOR)
				s = s[1:] // equivalent to s.pop(0)
			}
			U_debugF("  eatTillHeader: found %s%d%s content lines\n", S_BLUE, len(c), S_NOR)
			return c, s
		}

		// First call to eat_till_header
		_, ln = eatTillHeader(ln)
		U_debugF("After first eatTillHeader: lines length = %s%d%s\n", S_CYAN, len(ln), S_NOR)

		if len(ln) == 0 {
			U_debugF("No more lines, returning empty\n")
			return "", []string{}, []string{}
		}

		// Get header
		h := ln[0]
		ln = ln[1:] // equivalent to s.pop(0)
		U_debugF("Found header: %s%s%s\n", S_BLUE, h, S_NOR)

		// Get content
		c, ln := eatTillHeader(ln)
		U_debugF("Final result: h=\"%s%s%s\", c=%s%d%s items, s=%s%d%s remaining\n",
			S_GREEN, h, S_NOR, S_BLUE, len(c), S_NOR, S_CYAN, len(ln), S_NOR)

		return h, c, ln
	}
	U_infoF("Step 3: Starting to process headers with allowlist: %s%v%s\n", S_GREEN, L0, S_NOR)

	for len(ln) > 0 {
		U_debugF("--- Processing next section (remaining: %s%d%s lines) ---\n", S_CYAN, len(ln), S_NOR)
		h, c, ln = getNextHeaderContent(ln)

		if h == "" && len(c) == 0 {
			U_debugF("Skipping empty section\n")
			continue
		}

		U_debugF("Checking header \"%s%s%s\" against allowlist\n", S_BLUE, h, S_NOR)
		m := false
		for _, x := range L0 {
			if strings.Contains(h, x) {
				U_infoF("✓ Header matches \"%s%s%s\" - adding to output\n", S_GREEN, x, S_NOR)
				o = append(o, h)
				o = append(o, c...)
				U_infoF("Added header and %s%d%s content lines to output\n", S_BLUE, len(c), S_NOR)
				m = true
				break
			}
		}

		if !m {
			U_debugF("✗ Header \"%s%s%s\" not in allowlist - skipping\n", S_BLUE, h, S_NOR)
		}
	}

	return o
}
